#!/bin/sh

case "$1" in
  start)
    if pidof VBoxService &>/dev/null; then
      echo 'VBoxService is already running' >&2
      exit 1
    fi
    # Load the vbox modules
    if modprobe vboxguest &>/dev/null && modprobe vboxsf &>/dev/null; then
      # For timesync
      /sbin/VBoxService --timesync-set-start --timesync-set-threshold 10000 --disable-automount
    fi
    ;;
  stop)
    if ! pidof VBoxService &>/dev/null; then
      echo 'VBoxService is not running' >&2
      exit 1
    fi
    echo -n "Stopping VBoxService... "
    killall VBoxService &>/dev/null
    [ $? -eq 0 ] && echo "OK" || echo "FAIL"
    ;;
  restart)
    "$0" stop
    sleep 1
    "$0" start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    ;;
esac
